<?php
session_start();

require_once $_SERVER['DOCUMENT_ROOT'].'/actions/ActionFactory.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/const/Error.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/utils/Logger.php';

Logger::setLevel(Logger::DEBUG_LEVEL);

list($uri) = preg_split('/.html$|.html\?$/', $_SERVER['REQUEST_URI']);
$action = ActionFactory::getAction($uri);

$isRedirect = false;
$response = null;

if($action) {
	$response = $action->exec();
	if($response->getActionPath() !== null) {
		$isRedirect = true;
	}
}

if($isRedirect) {
	header('Location: http://'.$_SERVER['HTTP_HOST'].$response->getActionPath());
} else {
	include $_SERVER['DOCUMENT_ROOT'].'/view/pages/header.php';
	if($response !== null && $response->getError() !== null) {
		$errorMessage = $response->getError();
		include $_SERVER['DOCUMENT_ROOT'].'/view/pages/error.php';
	} else {
		if($response !== null && $response->getPagePath() !== null) {
			$uri = $response->getPagePath();
		}
		if(!(@include $_SERVER['DOCUMENT_ROOT'].'/view/pages'.$uri.'.php')) {
			$errorMessage = Error::PAGE_NOT_FOUND;
			include $_SERVER['DOCUMENT_ROOT'].'/view/pages/error.php';
		}
	}
	include $_SERVER['DOCUMENT_ROOT'].'/view/pages/footer.php';
}
?>